package cn.example.zjgsu.controller;

import cn.example.zjgsu.exception.MyException;
import cn.example.zjgsu.pojo.result.JsonData;
import cn.example.zjgsu.service.MyPageServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author: miaopeihan
 * @date: 2020-03-25  15:45
 */


@RestController
@RequestMapping({"/v1/api/mypage"})
@Api(description = "我的界面")
@CrossOrigin
public class MyPageController {

    @Autowired
    MyPageServer myPageServer;

    @ApiOperation("获取全国排名")
    @PostMapping(value = "/getNationalRank")
    //自己排名若为1001，说明统一1000+处理
    public JsonData getNationalRank(@RequestParam String account) {
        return JsonData.success(myPageServer.getNationalRank(account));
    }

    @ApiOperation("获取城市排名")
    @PostMapping(value = "/getCityRank")
    //自己排名若为1001，说明统一1000+处理
    public JsonData getCityRank(@RequestParam String account) {
        return JsonData.success(myPageServer.getCityRank(account));
    }

    @ApiOperation("获取个人主页")
    @PostMapping(value = "/getPersonInfo")
    public JsonData getPersonInfo(@RequestParam String account) {
        return JsonData.success(myPageServer.getPersonInfo(account));
    }

    @ApiOperation("获取收藏")
    @PostMapping(value = "/getFavorites")
    public JsonData getFavorites(@RequestParam String account) {
        return JsonData.success(myPageServer.getFavorites(account));
    }

    @ApiOperation("获取关注列表")
    @PostMapping(value = "/getFocusList")
    public JsonData getFocusList(@RequestParam String account) {
        return JsonData.success(myPageServer.getFocusList(account));
    }

    @ApiOperation("收藏食记")
    @PostMapping(value = "/collectFoodRecord")
    public JsonData collectFoodRecord(@RequestParam String account,@RequestParam int foodRecordId) {
        return JsonData.success(myPageServer.collectFoodRecord(account,foodRecordId));
    }

    @ApiOperation("不收藏食记")
    @PostMapping(value = "/disCollectFoodRecord")
    public JsonData disCollectFoodRecord(@RequestParam String account,@RequestParam int foodRecordId) {
        return JsonData.success(myPageServer.disCollectFoodRecord(account,foodRecordId));
    }

    @ApiOperation("关注某人")
    @PostMapping(value = "/focusPerson")
    public JsonData focusPerson(@RequestParam String myAccount,@RequestParam String focusAccount) {
        return JsonData.success(myPageServer.focusPerson(myAccount,focusAccount));
    }

    @ApiOperation("取关某人")
    @PostMapping(value = "/disFocusPerson")
    public JsonData disFocusPerson(@RequestParam String myAccount,@RequestParam String focusAccount) {
        return JsonData.success(myPageServer.disFocusPerson(myAccount,focusAccount));
    }

    @ApiOperation("获取收藏的食记")
    @PostMapping(value = "/getCollectionFoodRecord")
    public JsonData getCollectionFoodRecord(String account) {
        try {
            return JsonData.success(myPageServer.getCollectionFoodRecord(account));
        }catch (MyException e){
            return  JsonData.fail(e.getMessage());
        }
    }


    @ApiOperation("修改个人信息")
    @PostMapping(value = "/updatePersonalInformation")
    public JsonData updatePersonalInformation(MultipartFile file, HttpServletRequest request,@RequestParam("sex")String sex, @RequestParam("age")String age,@RequestParam("acount") String acount){
        try {
            return JsonData.success(myPageServer.fileUpload(file,request,sex,age,acount));
        } catch (IOException e) {
            e.printStackTrace();
            return JsonData.fail("上传失败");
        }
    }


}


